﻿using e_commerce.BLL.Services.Enterprise;
using e_commerce.Models;
using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using e_commerce.Filter;
using e_commerce.BLL.Models;

namespace e_commerce.Controllers
{
    public class RefundController : Controller
    {
        // GET: Refund

        private readonly OrderService _oservice;

        public RefundController()
        {
            TempData["f-nav"] = "退款管理";
            _oservice = new OrderService();
        }
        private void SetTempData(string fnav, string snav)
        {
            TempData["f-nav"] = fnav;
            TempData["s-nav"] = snav;
        }

        [PermissionFilter(permission: Permission.Server_Refund)]
        public ActionResult Refund(int page = 1)
        {
            SetTempData("退款管理", "退款管理");
            int pageSize = 8;
            List<RefundModel> list = _oservice.GetRefundByPage(page, pageSize).Select(l => new RefundModel()
            {
                GoodsName = l.Order.GoodsName,
                SumOfMoney = l.Order.Price * l.Order.Count,
                NickName = l.Client.User.NickName,
                RefundTime = l.CreateTime,
                RefundReason = l.Remark,
            }).ToList();
            return View(list.ToPagedList(page, pageSize));
        }


        /// <summary>
        /// 接受退款
        /// </summary>
        /// <param name="orderId">订单id</param>
        /// <returns></returns>
        public JsonResult AcceptRefund(int orderId)
        {
            return null;
        }

        [HttpPost]
        public ActionResult GetNewRefund()
        {
            TempData["url"] = "/Refund/AllRefund";
            TempData["icon"] = "/Image/icon_refund.png";
            TempData["name"] = "退款";
            TempData["title"] = "最新退款请求";
            var refunds = _oservice.GetNewRefund(5).Select(o => new AbbreviationsModel()
            {
                CreateTime = o.CreateTime.ToString(),
                Name = o.Order.GoodsName,
                Id = o.RefundId,
            });
            return PartialView("~/Views/Admin/ViewComponents/NewTips.cshtml", refunds);
        }
    }
}